﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;

namespace FYPDemo
{
    public partial class Member_Maintenance2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string name = Request.QueryString["name"].ToString();

                DataTable dt = Member.showMemberDetails(name);

                if (dt.Rows.Count > 0)
                {
                    lblName.Text = dt.Rows[0][1].ToString();
                    rblStatus.SelectedValue = dt.Rows[0][4].ToString();
                    lblEmail.Text = dt.Rows[0][5].ToString();
                    lblAddress.Text = dt.Rows[0][6].ToString();
                    lblPostCode.Text = dt.Rows[0][7].ToString();
                    lblState.Text = dt.Rows[0][8].ToString();
                    lblPhone.Text = dt.Rows[0][10].ToString();
                    DisplayPicture.ImageUrl = dt.Rows[0][12].ToString();
                }
            }
        }

        protected void btnChange_Click(object sender, EventArgs e)
        {
            int result = 0;
            int currentStatus = Convert.ToInt32(rblStatus.SelectedValue);
            string name = lblName.Text;

            if (currentStatus == 1)
            {
                result = BackOffice_Maintenance.updateUserStatus(name, 0);

                if (result > 0)
                {
                    Response.Redirect(Request.RawUrl);
                }
            }

            else
            {
                result = BackOffice_Maintenance.updateUserStatus(name, 1);

                if (result > 0)
                {
                    Response.Redirect(Request.RawUrl);
                }
            }
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            int result = 0;
            string name = lblName.Text;
            string email = lblEmail.Text;
            string charRnd = null;
            string numRnd = null;
            string resetPassword = null;

            charRnd = SecurityManagement.RandomString1();
            numRnd = SecurityManagement.RandomString2();
            resetPassword = charRnd + numRnd;

            result = SecurityManagement.ResetPassword(name, email, resetPassword);

            if (result > 0)
            {
                string subject = "Your Password successfully Reset";
                string body = "Dear Mr/Mrs " + name + ", <br /><br />  Kindly inform you that your password has been reset: ";
                body = body + "Please log in using the information below! <br/><br/>";
                body = body + "<b><span style=\"color:slateblue\">User Name: </span></b>" + name + "<br />" + "<br /> <br />";
                body = body + "<b><span style=\"color:slateblue\">New Password: </span></b>" + resetPassword + "<br />" + "<br /> <br />";
                body = body + "Thank you." + "<br />" + "<br />" + "<span style=\"font-style:italic; color:gray; font-size:11pt\"></span>";

                SendMail.SendEmail(email, subject, body);
            }
        }
    }
}